<?php

namespace plugin\ycMassage\app\api\controller;

use app\common\service\UploadService;
use plugin\ycMassage\app\model\MassageAds;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\utils\enum\State;
use support\Request;

class AdsController extends BaseController
{
    protected $noNeedLogin = ["list"];
    public function list(Request $request)
    {
        $position = $request->get('position');
        $saas_appid = $request->appid;
        if ($request->saas_appid) {
            $saas_appid = $request->saas_appid;
        }
        $MassageAdsObj = MassageAds::where(['position' => $position, 'saas_appid' => $saas_appid, 'state' => State::YES['value']]);
        $game = $request->get('game');
        if ($game) {
            $MassageAdsObj = $MassageAdsObj->whereRaw("`game` IS NULL OR `game` = '{$game}'");
        }
        $data = $MassageAdsObj->order('sort asc')->select()
            ->each(function ($item) {
                $item->image = UploadService::url($item->image);
                if ($item->image_2) {
                    $item->image_2 = UploadService::url($item->image_2);
                }
            });
        if ($data->isEmpty()) {
            return $this->fail('暂无广告');
        }
        return $this->successRes($data);
    }
}
